package com.carmanage.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.carmanage.dao.IEmployeesDao;
import com.carmanage.domain.Employees;
import com.carmanage.util.PageController;

@Component
public class EmployeesDaoImpl implements IEmployeesDao {
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void save(Employees employee) {
		Session session = sessionFactory.openSession();
		session.save(employee);
		session.close();
	}

	public List<Employees> showAllEmps(PageController pc) {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from Employees")
				.setFirstResult(pc.getPageStartRow())
				.setMaxResults(pc.getPageSize());
		List<Employees> Employees = query.list();
		session.close();
		return Employees;
	}

	public int getTotalRows() {
		int totalRow = 0;
		Session session = sessionFactory.openSession();
		Query query = session.createQuery(
				"select count(e) from Employees e");
		Object oj = query.list().get(0);
		Number num = (Number) oj;
		totalRow = num.intValue();
		session.close();
		return totalRow;
	}

	public void deleteEmpById(int Id) {
		Session session = sessionFactory.openSession();
		Query query = session
				.createQuery("delete from Employees e where e.id=?");
		query.setInteger(0, Id);
		query.executeUpdate();
		session.close();
	}

	public List<Employees> groupSearch(Employees employee, PageController pc) {
		String searchName = employee.getName();
		int searchEmpId = employee.getEmployee_id();
		String searchDept = employee.getDepartment();
		int searchClasses = employee.getRuns_id();
		String station = employee.getStationname();
		Session session = sessionFactory.openSession();
		Query query = session
				.createQuery(
						"from Employees e where e.name=? and e.employee_id=? and "
								+ "e.department=? and e.runs_id=? and e.stationname=?")
				.setFirstResult(pc.getPageStartRow())
				.setMaxResults(pc.getPageSize());
		query.setString(0, searchName);
		query.setInteger(1, searchEmpId);
		query.setString(2, searchDept);
		query.setInteger(3, searchClasses);
		query.setString(4, station);
		List<Employees> Employees = query.list();
		session.close();
		return Employees;
	}

	public int getGroupTotalRows(Employees employee) {
		String searchName = employee.getName();
		int searchEmpId = employee.getEmployee_id();
		String searchDept = employee.getDepartment();
		int searchClasses = employee.getRuns_id();
		String station = employee.getStationname();
		int totalRow = 0;
		Session session= sessionFactory.openSession();
		Query query = session.createQuery(
				"select count(e) from Employees e where e.name=? and e.employee_id=? and "
						+ "e.department=? and e.runs_id=? and e.stationname=?");
		query.setString(0, searchName);
		query.setInteger(1, searchEmpId);
		query.setString(2, searchDept);
		query.setInteger(3, searchClasses);
		query.setString(4, station);
		Object oj = query.list().get(0);
		Number num = (Number) oj;
		totalRow = num.intValue();
		session.close();
		return totalRow;
	}

	public List<Object[]> getClasses() {
		Session session = sessionFactory.openSession();
		Query query = session
				.createQuery("select distinct e.runs_id from Employees e");
		List<Object[]> classes = query.list();
		session.close();
		return classes;
	}

	public List<Object[]> getDepts() {
		Session session = sessionFactory.openSession();
		Query query = session
				.createQuery("select distinct e.department from Employees e");
		List<Object[]> depts = query.list();
		session.close();
		return depts;
	}

	public List<Object[]> getStations() {
		Session session = sessionFactory.openSession();
		Query query = session
				.createQuery("select distinct e.stationname from Employees e");
		List<Object[]> stations = query.list();
		session.close();
		return stations;

	}

	public Employees showModifyData(int modifyEmpId) {
		Session session = sessionFactory.openSession();
		Query query = session
				.createQuery("select e from Employees e where e.employee_id=?");
		query.setInteger(0, modifyEmpId);
		Employees modifyEmps = (Employees) query.uniqueResult();
		session.close();
		return modifyEmps;
	}

	public void modiEmp(int modiID, Employees employee) {
		String modiName = employee.getName();
		int modiEmpId = employee.getEmployee_id();
		String modiDept = employee.getDepartment();
		int modiClass = employee.getRuns_id();
		int modiGroup = employee.getGroup_id();
		String modiStation = employee.getStationname();
		Session session = sessionFactory.openSession();
		String hql = "Update Employees e set e.name=?,e.employee_id=?,e.department=?,e.Group_id=?,e.stationname=?,e.runs_id=? where e.id=?";
		Query query = session.createQuery(hql);
		query.setString(0, modiName);
		query.setInteger(1, modiEmpId);
		query.setString(2, modiDept);
		query.setInteger(3, modiGroup);
		query.setString(4, modiStation);
		query.setInteger(5, modiClass);
		query.setInteger(6, modiID);
		query.executeUpdate();
		session.close();
	}
}
